﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace WebApplication.Admins.Controls
{
    public partial class UCEditTaiKhoan : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Display();
            }

        }
        public void Display()
        {
            dllphanquen.DataValueField = "IDQuen";
            dllphanquen.DataTextField = "TenQuen";
            dllphanquen.DataSource = NhomQuyenBLL.LayTatCa();
            dllphanquen.DataBind();

            string cid = Request.QueryString["Id"] ?? "";
            if (cid != "")
            {

                Entities.TaiKhoan data = TaiKhoanBLL.LayID(cid);
                if (data == null)
                    Response.Redirect("~/Admin/TaiKhoan.aspx");
                lblTitle.Text = "Quản Lý Tài Khoản";
                lblId.Text = data.IDTaiKhoan.ToString();
                txtTenTaiKhoan.Text = data.TenTaiKhoan;
                txtMatKhau.Text = data.MatKhau;
                txthoten.Text = data.HoTen;
                txtNgaySinh.Text = data.NgaySinh.ToString("dd/MM/yyyy");
                if (data.GioiTinh == 1)
                    rdNam.Checked = true;
                else
                    rdNu.Checked = true;
                txtEmail.Text = data.Email;
                if (data.TrangThai == 1)
                    rdTrue.Checked = true;
                else
                    rdFalse.Checked = true;
                dllphanquen.SelectedValue = data.IDTaiKhoan.ToString();
                btnReset.Visible = false;
   
            }
            else
            {
                lblTitle.Text = "Thêm Tài Khoản";
                btnReset.Visible = true;
            }
        }
        public Entities.TaiKhoan getData()
        {
            Entities.TaiKhoan data = null;
            if (lblId.Text != "")
                data = BLL.TaiKhoanBLL.LayID(lblId.Text);
            else
                data = new Entities.TaiKhoan();
            data.TenTaiKhoan=txtTenTaiKhoan.Text;
            data.MatKhau = txtMatKhau.Text;
            data.HoTen =txthoten.Text;
            data.NgaySinh = DateTime.Parse(txtNgaySinh.Text);
            if (rdNam.Checked == true)
               data.GioiTinh = 1;
            else
                data.GioiTinh = 0;
            data.Email = txtEmail.Text;
            if (rdTrue.Checked == true)
                data.TrangThai = 1;
            else
                data.TrangThai = 0;
            data.PhanQuen = Convert.ToInt32(dllphanquen.SelectedValue);
            return data;
        }
        private void Reset()
        {
            txtTenTaiKhoan.Text = "";
            txtMatKhau.Text = "";
            txthoten.Text = "";
            rdNam.Checked = true;
            rdNu.Checked = false;
            rdTrue.Checked = true;
            rdFalse.Checked = false;
            txtEmail.Text = "";
            txtNgaySinh.Text = DateTime.Now.ToString("dd/MM/yyyy");
            dllphanquen.SelectedValue = "0";
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool rs = false;
                Entities.TaiKhoan data = getData();
                if (data.IDTaiKhoan > 0)
                {
                    rs = BLL.TaiKhoanBLL.Sua(data);
                    lblStatus.Text = rs ? "Update thành công" : "Update thất bại";
                }
                else
                {
                    rs = BLL.TaiKhoanBLL.Them(data);
                    lblStatus.Text = rs ? "Insert thành công" : "Insert thất bại";
                    if (rs)
                        Reset();
                }
            }
        }

    }
}